﻿using System;
using Chat = System.Net;
using ChatClientApp.Tools;
using System.IO;

namespace ChatClientApp.Classes
{
    class ClientLogin : IDisposable
    {
        string nick,
               pw;
        Chat.Sockets.TcpClient tcpClient;
        bool disposed;
        bool isLogedIn;
        bool finishedLogin;
        string[] contactList;

        public string[] ContactList
        {
            get { return contactList; }
        }
        public bool FinishedLogin
        {
            get { return finishedLogin; }
        }
        public bool IsLogedIn
        {
            get { return isLogedIn; }
        }
        public string Nick
        {
            get { return nick; }
        }
        int serverPort;

        public delegate void SendMessageEventHandler(object sender, MessageEventArgs mEA);
        public static event SendMessageEventHandler OnSendMessageHandler;

        public ClientLogin(string nick, string pw, int port, Chat.Sockets.TcpClient tcpClient)
        {
            this.nick = nick;
            this.pw = pw;
            this.serverPort = port;
            this.tcpClient = tcpClient;
            this.disposed = false;
            this.isLogedIn = false;
            this.finishedLogin = false;
        }

        public void Login()
        {
            raiseEvent("Trying to login...");
            StreamReader reader = new StreamReader(tcpClient.GetStream());
            StreamWriter writer = new StreamWriter(tcpClient.GetStream());
            string temp = reader.ReadLine();
            writer.WriteLine("LOGIN");
            writer.Flush();
            temp = reader.ReadLine();
            if (temp == "ACK")
            {
                writer.WriteLine(nick);
                writer.WriteLine(pw);
                writer.WriteLine(serverPort.ToString());
                writer.Flush();
            }

            if (reader.ReadLine() == "ACK")
                this.isLogedIn = true;
            else
                this.isLogedIn = false;

            this.finishedLogin = true;

            temp = reader.ReadLine();

            raiseEvent(temp);

            this.contactList = reader.ReadLine().ToAsciiString("|");

        }

        public void Logout()
        {
            raiseEvent("Trying to logout...");
            StreamReader reader = new StreamReader(tcpClient.GetStream());
            StreamWriter writer = new StreamWriter(tcpClient.GetStream());
            string temp = reader.ReadLine();
            writer.WriteLine("LOGOUT");
            writer.Flush();
            temp = reader.ReadLine();
            if (temp == "ACK")
            {
                writer.WriteLine(nick);
                writer.Flush();
            }

            if (reader.ReadLine() == "ACK")
                this.isLogedIn = false;
            else
                this.isLogedIn = true;
            temp = reader.ReadLine();

            raiseEvent(temp);
        }

        private void raiseEvent(String mess)
        {
            this.OnSendMessage(new MessageEventArgs(mess));
        }

        protected void OnSendMessage(MessageEventArgs e)
        {
            if (OnSendMessageHandler != null)
                OnSendMessageHandler(this, e);
        }

        #region Disposing
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
          
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    this.nick = null;
                    this.pw = null;
                    this.tcpClient = null;
                }
                disposed = true;
            }
        }

        ~ClientLogin()
        {
            // Simply call Dispose(false).
            Dispose (false);
        }
        #endregion
    }

}
